// pages/restaurant/cook/cookDetail/cookDetail.js
const util = require('../../../utils/util');
import { find, save } from '../../../api/chef'
import { getInfo } from '../../../api/member'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberId: '',
    cookId: '',
    shopInfo: {},
    cookInfo: {},
    APP_OSSURL_API: app.APP_OSSURL_API,
  },
  loadMember() {
    getInfo().then(res => {
      this.data.memberId = res.data.member&&res.data.member.id
    })
  },
  zanClick() {
    var form = {
      memberId: this.data.memberId,
      chefId: this.data.cookId
    }
    save(form).then(res => {
      if (res.type == 'success') {
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 1000
        })
      }
      this.loadCook(this.data.cookId)
    })
  },
  toRelativeShop() {
    wx.navigateTo({
      url: '/pages/shop/shopDetail/index?shopId=' + this.data.shopInfo.id + '&mchId=' + app.globalData.mchId
    })
  },
  loadCook(data) {
    var params = {
      lng: app.globalData.lng,
      lat: app.globalData.lat
    }
    find(data, params).then(res => {
      res.data.chef.starts = []
      res.data.shopVo.starts = []
      res.data.shopVo.score = res.data.shopVo.score.toFixed(1)
      res.data.shopVo.score = '5.0' //TODO 默认五星
      res.data.shopVo.logo = util.thumbnail(res.data.shopVo.logo, 160, 118)
      res.data.shopVo.distance = util.rateConverter(res.data.shopVo.distance)
      for (var k = 0; k < res.data.shopVo.score; k++) {
        res.data.shopVo.starts.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/ic_list_star_red%402x.png')
      }
      for (var x = res.data.shopVo.starts.length; x < 5; x++) {
        res.data.shopVo.starts.push('http://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/canyin/canyin-pingfen-gray.png')
      }
      this.data.shopInfo = res.data.shopVo
      res.data.chef.createDate = util.datetimeFormat(res.data.chef.createDate)
      res.data.chef.avatar = util.thumbnail(res.data.chef.avatar, 258, 336)
      res.data.chef.chefMeta = res.data.chef.chefMeta.map(e => {
        return{
          ...e,
          thumbnail:util.thumbnail(e.thumbnail, 54, 54)
        }
      })
      for (var i = 0; i < res.data.chef.score; i++) {
        res.data.chef.starts.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/ic_list_star_red%402x.png')
      }
      // TOOD 非五星时的灰色星星显示
      this.data.cookInfo = res.data.chef
      this.setData({
        shopInfo: this.data.shopInfo,
        cookInfo: this.data.cookInfo
      })

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.info("options======================" + options)
    console.info(options)
    this.loadMember()
    this.loadCook(options.cookId)

    this.setData({
      cookId: options.cookId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 用户点击右上角分享
   */

})